package org.openzip.jarsfx.unpacking.view;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.jar.JarEntry;

/**
 * This class is inherited from File class and therefore can be used within
 * unpacking gui and its tree structures. It envelopes JarEntry class from which
 * can be determined whether it is file or directory. This is done by
 * overloading the isFile() and isDirectory() functions on File class. Class can
 * also accept child files by calling addJarFile(MyJarFile jarFile). These files
 * can by than obtained by File[] listFiles() function.
 * 
 * @author David �unek
 * 
 */
public class MyJarFile extends File {
	private ArrayList<MyJarFile> children = new ArrayList<MyJarFile>();
	private ArrayList<MyJarFile> childrenFolders = new ArrayList<MyJarFile>();
	private JarEntry jarEntry;

	/**
	 * Constructor which envelopes JarEntry into File structure.
	 * 
	 * @param JarEntry
	 *            to be enveloped within this structure.
	 */
	public MyJarFile(JarEntry jarEntry) {
		super(jarEntry == null ? "\\" : jarEntry.getName());
		this.jarEntry = jarEntry;
	}

	/**
	 * Function returns File[] structure with all files stored by calling
	 * addJarFile(MyJarFile jarFile) function. That is all subdirectories and
	 * files.
	 */
	public File[] listFiles() {
		File[] f = new File[children.size() + childrenFolders.size()];
		for (int i = 0; i < childrenFolders.size(); i++) {
			f[i] = childrenFolders.get(i);
		}
		for (int i = childrenFolders.size(); i < f.length; i++) {
			f[i] = children.get(i - childrenFolders.size());
		}
		for (int i = 0; i < f.length; i++) {
			System.out.println(f[i].getName());
		}
		return f;
	}

	/**
	 * This function is similar to listFiles except this return array of
	 * strings.
	 */
	public String[] list(FilenameFilter filter) {
		Collection<String> col = new ArrayList<String>();
		for (MyJarFile f : childrenFolders) {
			if (filter.accept(this, f.getName())) {
				col.add(getName());
			}
		}
		for (MyJarFile f : children) {
			if (filter.accept(this, f.getName())) {
				col.add(getName());
			}
		}
		return col.toArray(new String[col.size()]);
	}

	/**
	 * This overloads the formal isFile() function on File class and returns
	 * value based on enveloped JarEntry.
	 */
	public boolean isFile() {
		return !jarEntry.isDirectory();
	}

	/**
	 * This overloads the formal isDirectory() function on File class and
	 * returns value based on enveloped JarEntry.
	 */
	public boolean isDirectory() {
		return jarEntry.isDirectory();
	}

	/**
	 * Function is used to add MyJarFile (that is the same stucture as this one)
	 * into this structure as its child file. All child files can be obtained by
	 * calling listFiles() function.
	 * 
	 * @param MyJarFile
	 *            to be added as a child file of this structure.
	 */
	public void addJarFile(MyJarFile jarFile) {
		if (jarFile.isDirectory()) {
			childrenFolders.add(jarFile);
		} else {
			children.add(jarFile);
		}
	}

	public String getEntryPath() {
		return jarEntry == null ? "\\" : jarEntry.getName();
	}

	public JarEntry getEntry() {
		return jarEntry;
	}
}
